<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Quick Start</title>
  <script src="/js/spritejs.js"></script>
  <style>
    html,body {
      margin: 0;
      padding: 0;
      background: #201e1e;
      height: 100%;
      width: 100%;
      max-width: 800px;
    }

    #stage {
      display: inline-block;
      width: 100%;
      height: 0;
      padding-bottom: 100%;
      background: #201e1e;
    }  
  </style>
</head>
<body>
  <div id="stage"></div>
  <script>
  const {Scene, Sprite, Label, Group} = spritejs;
  const container = document.getElementById('stage');
  const scene = new Scene({
    container,
    width: 1200,
    height: 1200,
  });

  const fglayer = scene.layer('fglayer');
  fglayer.canvas.style.backgroundColor = '#1EAC61';

  const button = new Group();
  button.attr({
    anchor: 0.5,
    pos: [600, 600],
  });
  fglayer.append(button);

  const buttonNormal = new Label('Get Started');
  buttonNormal.attr({
    anchor: 0.5,
    font: '40px "宋体"',
    fillColor: '#04773B',
    lineHeight: 96,
    textAlign: 'center',
    size: [320, 96],
    border: [4, '#178C4E'],
    borderRadius: 48,
  });
  button.append(buttonNormal);

  const buttonHover = new Sprite();
  buttonHover.attr({
    anchor: 0.5,
    bgcolor: '#208B50',
    height: 100,
    width: 96,
    opacity: 0,
    borderRadius: 48,
    zIndex: -1,
  });
  button.append(buttonHover);

  /* eslint-disable no-console */
  console.log('Place mouse hover the button...');
  /* eslint-enable no-console */

  let hoverAnim = null;

  let touched = false;
  // prevent mouseenter on mobile device
  // mobile point events: touchstart > (touchmove)+ > touchend > (mouseenter) > mouseover > mousemove > mousedown > mouseup > click
  // https://patrickhlauke.github.io/touch/tests/results/
  buttonNormal.addEventListener('mouseenter', async (evt) => {
    if(touched) return;
    fglayer.canvas.style.cursor = 'pointer';
    buttonNormal.attr({
      fillColor: '#fff',
    });
    if(hoverAnim) {
      hoverAnim.cancel();
      hoverAnim = null;
    }
    hoverAnim = buttonHover.animate([
      {width: 96, opacity: 0},
      {width: 324, opacity: 1},
    ], {
      duration: 300,
      fill: 'forwards',
      easing: 'ease-in',
    });
    await hoverAnim.finished;
    hoverAnim = null;
  });
  
  buttonNormal.addEventListener('mouseleave', async (evt) => {
    if(touched) return;
    fglayer.canvas.style.cursor = 'default';
    buttonNormal.attr({
      fillColor: '#04773B',
    });
    if(hoverAnim) {
      hoverAnim.cancel();
      hoverAnim = null;
    }
    hoverAnim = buttonHover.animate([
      {width: 324, opacity: 1},
      {width: 96, opacity: 0},
    ], {
      duration: 500,
      fill: 'forwards',
      easing: 'ease-out',
    });
    await hoverAnim.finished;
    hoverAnim = null;
  });

  buttonNormal.addEventListener('touchstart', (evt) => {
    touched = true;
    if(hoverAnim) {
      hoverAnim.cancel();
      hoverAnim = null;
    }
    buttonNormal.attr({
      fillColor: '#fff',
    });
    buttonHover.attr({
      width: 324,
    });
  });

  buttonNormal.addEventListener('touchend', (evt) => {
    if(hoverAnim) {
      hoverAnim.cancel();
      hoverAnim = null;
    }
    buttonNormal.attr({
      fillColor: '#04773B',
    });
    buttonHover.attr({
      width: 0,
    });
  });

  buttonNormal.addEventListener('mousedown', (evt) => {
    button.attr({
      scale: 0.9,
    });
  });
  
  buttonNormal.addEventListener('mouseup', (evt) => {
    button.attr({
      scale: 1.0,
    });
  });

  buttonNormal.addEventListener('click', (evt) => {
    /* eslint-disable no-console */
    console.log('button clicked');
    /* eslint-enable no-console */
  });
  </script>
</body>
</html>